<#include "/common/_layout.html"/>
<@layout>
<!-- <h1>定时任务管理&nbsp;&nbsp;
<a href="/quartz/add">创建定时任务</a>
</h1> -->
<script>
	$(function(){
		//初始化列表
		$('#dg').datagrid({
			url:'/quartz/paginate',
			rownumbers:true,
			pagination:true,
			pageSize:10,
			toolbar:'#td',
			singleSelect:true,
			columns:[[
				{field:'id',title:'id',hidden:true},
				{field:'job_class_name',title:'任务类名'},
				{field:'cron_expression',title:'cron表达式'},
				{field:'job_status',title:'任务状态',
					formatter: function(value,row,index){
						if ('0' == value){
							return '暂停';
						} else {
							return '运行中';
						}
					}},
				{field:'job_info',title:'任务描述',width:260}
			]],
			onSelect:function(index, row) {
				//恢复所有按钮
				setButtons(['btn_start','btn_pause','btn_edit','btn_remove'],false);
				//根据状态禁用相应的按钮
				if(1 == row.job_status || 3 == row.job_status) {
					setButtons(['btn_start','btn_edit','btn_remove'],true);
				} else if (0 == row.job_status) {
					setButtons(['btn_pause'],true);
				} else {
					setButtons(['btn_start','btn_pause','btn_edit','btn_remove'],true);
				}
			}
		});
		
		//初始化表单
		$('#ff').form({
			success:function(data) {
				var obj = eval('(' + data + ')');
				if(obj.success) {
					$('#form_win').window('close');
					$.messager.alert('提示', obj.msg);
					$('#dg').datagrid('reload');
				}
			}
		});
	});

	function openAddWindow() {
		$('#form_win').attr('title', '添加定时任务');
		clearForm();
		$('#ff').form({
			url:'/quartz/save'
		});
		$('#form_win').window('open');
		
	}
	
	function openEditWindow() {
		//获取选中的行
		var row = $('#dg').datagrid('getSelected');
		if(row == null) {
			$.messager.alert('错误','请选择数据后再进行修改!');
			return;
		}
		
		page_ajax('/quartz/edit/' + row.id, 'editAfter', '');
	}
	
	function editAfter(msg) {
		$('#form_win').attr('title', '编辑定时任务');
		$('#form_win').window('open');
		clearForm();
		$('#ff').form({
			url:'/quartz/update'
		});
		for(var p in msg) {
			$('#' + p).textbox('setValue', msg[p]);
		}
	}
	
	function startJob() {
		//获取选中的行
		var row = $('#dg').datagrid('getSelected');
		if(row == null) {
			$.messager.alert('错误','请选择数据后再进行操作!');
			return;
		}
		
		page_ajax('/quartz/resume/' + row.id, 'ajaxAfter', '');
	}
	
	function pauseJob() {
		//获取选中的行
		var row = $('#dg').datagrid('getSelected');
		if(row == null) {
			$.messager.alert('错误','请选择数据后再进行操作!');
			return;
		}
		
		page_ajax('/quartz/pause/' + row.id, 'ajaxAfter', '');
	}
	
	function delJob() {
		//获取选中的行
		var row = $('#dg').datagrid('getSelected');
		if(row == null) {
			$.messager.alert('错误','请选择数据后再进行操作!');
			return;
		}
		
		page_ajax('/quartz/delete/' + row.id, 'ajaxAfter', '');
	}
	
	function ajaxAfter(msg) {
		if(msg.success) {
			$.messager.alert('提示',msg.msg);
			$('#dg').datagrid('reload');
		} else {
			$.messager.alert('错误',msg.errMsg);
		}
	}
	
	//操作按钮
	function setButtons(btnIds, status) {
		for(var i=0; i<btnIds.length; i++) {
			$('#' + btnIds[i]).linkbutton({
				disabled:status
			});
		}
	}
	
	function submitForm(){
		$('#ff').form('submit');
	}
	function clearForm(){
		$('#ff').form('clear');
	}
	
	//立即运行一次
	function runJobOnce() {
		//获取选中的行
		var row = $('#dg').datagrid('getSelected');
		if(row == null) {
			$.messager.alert('错误','请选择数据后再进行操作!');
			return;
		}
		
		page_ajax('/quartz/runOnce/' + row.id, 'ajaxAfter', '');
	}
</script>
<div class="table_box">
	
	<!-- 导入工具栏 -->
	<#include "/common/_toolbar.html"/>
	<@toolbar toolbarArr = [['btn_add','icon-add','openAddWindow()','添加任务']
	                       ,['btn_edit','icon-edit','openEditWindow()','修改任务']
	                       ,['btn_start','icon-start','startJob()','开始任务']
	                       ,['btn_pause','icon-pause','pauseJob()','暂停任务']
	                       ,['btn_start','icon-start','runJobOnce()','立即运行任务']
	                       ,['btn_remove','icon-remove','delJob()','删除任务']]
	          optionArr = []/>
</div>

<table id="dg" style="height: 730px"></table>

<!-- 添加页面 -->
<div id="form_win" class="easyui-window" title="添加定时任务" style="width:600px;height:400px"
        data-options="iconCls:'icon-save',modal:true,closed:true">
    <div style="padding:10px 60px 20px 60px">
    <form id="ff" method="post">
	    <table cellpadding="5">
	    	<tr>
	    		<td>任务名称:</td>
	    		<td><input class="easyui-textbox" type="text" id="job_name" name="quartzJob.job_name" data-options="required:true,width:260"></input></td>
	    	</tr>
	    	<tr>
	    		<td>任务分组:</td>
	    		<td><input class="easyui-textbox" type="text" id="job_group" name="quartzJob.job_group" data-options="required:true,width:260"></input></td>
	    	</tr>
	    	<tr>
	    		<td>对应的任务名称:</td>
	    		<td><input class="easyui-textbox" type="text" id="job_class_name" name="quartzJob.job_class_name" data-options="required:true,width:260"></input></td>
	    	</tr>
	    	<tr>
	    		<td>cron表达式:</td>
	    		<td><input class="easyui-textbox" type="text" id="cron_expression" name="quartzJob.cron_expression" data-options="required:true,width:260"></input></td>
	    	</tr>
	    	<tr>
	    		<td>任务描述:</td>
	    		<td><input class="easyui-textbox" type="text" id="job_info" name="quartzJob.job_info" data-options="multiline:true,width:260" style="height: 60px"></input></td>
	    	</tr>
	    </table>
	    
	    <input class="easyui-textbox" type="hidden" id="id" name="quartzJob.id"/>
    </form>
    <div style="text-align:center;padding:5px">
    	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()">提交</a>
    	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()">清空</a>
    </div>
    </div>
</div>
</@layout>